#include "render_point.h"
#include "qbe/src/layout/layout.hpp"

namespace qbe::utils::TempRender {

RenderPoint::RenderPoint(vsg::ref_ptr<vsg::Options> options, std::vector<qlib::Label::s_ptr> points) : m_points(points) {
  switchObject = vsg::Switch::create();
  transform = vsg::MatrixTransform::create();
  switchObject->addChild(true, transform);

  Mesh2D merge_result;
  merge_result.vertices = vsg::vec2Array::create(points.size());
  merge_result.indices = vsg::uintArray::create(points.size());
  for (uint32_t i = 0; i < points.size(); i++) {
    (*merge_result.vertices)[i] = vsg::vec2{float(points[i]->origin.x), float(points[i]->origin.y)};
    (*merge_result.indices)[i] = i;
  }

  PointShaderMaterial2D material{Point_Uniform{vsg::vec4(1.0, 0.0, 0.0, 1.0), 1, 10}, "shaders/cell/key_point.vert.spv",
                                 "shaders/cell/key_point.frag.spv"};

  point_node = PointMeshNode::create(options, merge_result, material, VkPrimitiveTopology::VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
  transform->addChild(point_node);
}

RenderPoint ::~RenderPoint() {}

void RenderPoint::setLayerType(vsg::vec4 color, int point_type, double point_size) {
  point_node->setLayerType(color, point_type, point_size);
}

void RenderPoint::updateData(std::vector<qlib::Label::s_ptr> points) {
  if (points.empty())
    return;
  m_points = points;
  Mesh2D merge_result;
  merge_result.vertices = vsg::vec2Array::create(points.size());
  merge_result.indices = vsg::uintArray::create(points.size());
  for (uint32_t i = 0; i < points.size(); i++) {
    (*merge_result.vertices)[i] = vsg::vec2{float(points[i]->origin.x), float(points[i]->origin.y)};
    (*merge_result.indices)[i] = i;
  }
  point_node->mesh = merge_result;
  point_node->update();
}

void RenderPoint::setShowHide(bool is_show) {
  switchObject->setAllChildren(is_show);
}

}  // namespace qbe::utils::TempRender